package zzu.mxd.answer.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import zzu.mxd.common.R;
import zzu.mxd.utils.RedisUtils;
import zzu.mxd.utils.VerifyCodeUtil;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.util.concurrent.TimeUnit;

/**
 * Created by MXD on 2018/11/9.
 */
@Api(tags = "验证码")
@RestController
@RequestMapping("/VerifyController")
public class VerifyController {

    @Autowired
    RedisUtils redisUtils;

    /**
     * 获取验证码图片
     * @param request
     * @param response
     * @param id 学生ID
     * @throws Exception
     */
    @ApiOperation("获取验证码图片")
    @RequestMapping(value = "/VC", method = RequestMethod.GET)
    @ResponseBody
    public void  VC(HttpServletRequest request,HttpServletResponse response,String id) throws Exception{
        VerifyCodeUtil code=new VerifyCodeUtil();//生成验证码
        BufferedImage image = code.getImage();//生成验证码图片
        //将验证码文本存到Redis
        redisUtils.set("verifyCode"+id,code.getText(),1L, TimeUnit.MINUTES);
        ImageIO.write(image,"jpg",response.getOutputStream());
    }

    /**
     * 获取验证码的key
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @ApiOperation("获取验证码的key")
    @RequestMapping(value = "/VCKey", method = RequestMethod.POST)
    @ResponseBody
    public R<String> VCkey(HttpServletRequest request, HttpServletResponse response ) throws Exception{
        return R.succC("verifyCode");
    }

}
